/* 
 * Copyright (C) 2012 UTSChedule
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package utsc.apps.utschedule.services;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

/**
 * A simple client for setting up an alarm for assignment.
 */
public class ReminderClient {
	private ReminderService _boundService;
	private boolean _bound;
	private Context _context;
	
	public ReminderClient(Context context){
		this._context=context;
	}
	
	/**
     * Call this to connect your activity to your service
     */
    public void doBindService() {
        // Establish a connection with our service
        _context.bindService(new Intent(_context, ReminderService.class), _connection, Context.BIND_AUTO_CREATE);
        _bound = true;
    }
     
    /**
     * When you attempt to connect to the service, this connection will be called with the result.
     * If we have successfully connected we instantiate our service object so that we can call methods on it.
     */
    private ServiceConnection _connection = new ServiceConnection() {
        @Override
    	public void onServiceConnected(ComponentName className, IBinder service) {
            // This is called when the connection with our service has been established, 
            // giving us the service object we can use to interact with our service.
            _boundService = ((ReminderService.ServiceBinder) service).getService();
        }
 
        @Override
        public void onServiceDisconnected(ComponentName className) {
            _boundService = null;
        }

    };
    
    /**
     * Tell our service to set an alarm for the given date
     * @param c a date to set the notification for
     */
    public void setAlarmForNotification(long date,String identifier){
        _boundService.setAlarm(date,identifier);
    }
     
    /**
     * When you have finished with the service call this method to stop it 
     * releasing your connection and resources
     */
    public void doUnbindService() {
        if (_bound) {
            // Detach our existing connection.
            _context.unbindService(_connection);
            _bound = false;
        }
    }
}
